package com.logic.modular.game.player.bag.service;

import cn.hutool.core.lang.Pair;

import com.comment.pojo.domain.bag.BagItem;
import com.comment.pojo.domain.logic.core.CoreConfig;
import com.logic.comment.util.RedisUtils;
import com.logic.modular.game.player.bag.dto.UsePropDto;
import com.logic.modular.game.player.bag.service.handler.PropType2;
import com.logic.modular.game.player.bag.service.handler.PropType2Handler;
import com.logic.modular.gameCore.config.service.impl.CoreConfigServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 背包物品服务---道具服务
 */
@Slf4j
@Service

public class PlayerPropType2ServiceImpl {
    @Resource
    private BagItemServiceImpl bagItemService;
    @Resource
    private CoreConfigServiceImpl coreConfigServiceImpl;
    @Resource
    private RedisUtils redisUtils;


    public boolean tryPayProp(Long playerId, Long corePropId, BigDecimal num) {
        boolean isPay = false;
        if (bagItemService.checkPropNum(playerId, corePropId, num)) {
            isPay = bagItemService.obtainBagItem(playerId, corePropId, num.negate());
        }
        return isPay;
    }

    public void useProp(UsePropDto dto) {
        Long uid = dto.getUid();
        Long corePropId = dto.getCorePropId();
        BigDecimal num = dto.getNum();

        boolean isPay = tryPayProp(uid, corePropId, num);

        if (isPay) {
            PropType2Handler handler = PropType2.getHandler(corePropId);
            if (handler == null) {
                log.error("未找到道具处理器:{}", corePropId);
                return;
            }
            handler.handler(dto);
        }
    }


    private static final String key1 = "seasonStartTime";
    private static final String key2 = "seasonEndTime";

    public boolean noneMatchSeason(Long playerId, Long bagItemId) {
        // 赛季刷新
        Pair<Long, Long> longLongPair = obtainSeasonDay();
        long seasonStartTime =longLongPair.getKey();
        long seasonEndTime = longLongPair.getValue();

        CoreConfig byId = coreConfigServiceImpl.getById(12);
        Long seasonCard = Long.valueOf(byId.getValue2());
        BagItem bagItem = bagItemService.getById(bagItemId);
        Long corePropId = bagItem.getCorePropId();
        if (!seasonCard.equals(corePropId)) {
            throw new RuntimeException("当前物品不是赛季卡");
        }


        // 用户战令卡有效刷新
        Date time = (Date) bagItem.extend.get("obtainTime");
        long termOfValidity = time.getTime();
        if (seasonStartTime > termOfValidity | termOfValidity > seasonEndTime) {// 过期
            // 道具过期了 , 删除掉
            bagItemService.obtainBagItem(playerId, corePropId, BigDecimal.ONE.negate());
            throw new RuntimeException("战令卡已过期");
        }
        return true;
    }

    /**
     * 获取赛季开始结束时间
     */
    public Pair<Long, Long> obtainSeasonDay() {
        // 获取当前生效的战令卡物品id
        CoreConfig byId = coreConfigServiceImpl.getById(12);
        int resetDay = Integer.parseInt(byId.getValue3());
        long resetDayLong = (long) resetDay * 24 * 60 * 60 * 1000;


        // 赛季刷新
        long seasonStartTime;
        long seasonEndTime;
        if (!redisUtils.hasKey(key1)) {// 初始化赛季
            seasonStartTime = new Date().getTime();
            seasonEndTime = seasonStartTime + resetDayLong;
            redisUtils.set(key1, seasonStartTime);
            redisUtils.set(key2, seasonEndTime);
        } else { // 刷新赛季
            seasonStartTime = (long) redisUtils.get(key1);
            seasonEndTime = (long) redisUtils.get(key2);
            if (seasonEndTime < System.currentTimeMillis()) {
                seasonStartTime = new Date().getTime();
                seasonEndTime = seasonStartTime + resetDayLong;
                redisUtils.set(key1, seasonStartTime);
                redisUtils.set(key2, seasonEndTime);
            }
        }
        return Pair.of(seasonStartTime, seasonEndTime);
    }

}
